package com.pipayshop.controller;


import com.pipayshop.entity.Image;
import com.pipayshop.entity.dto.BgImgDTO;
import com.pipayshop.entity.vo.BgImgVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.mapper.ImageMapper;
import com.pipayshop.service.BgImgService;
import com.pipayshop.utils.OssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * item/shop的首页背景轮播图数据 前端控制器
 * </p>
 *
 * @author nws
 * @since 2023-07-25
 */
@Api(value = "店首页背景图片接口",tags = "店首页背景图片接口")
@RestController
@RequestMapping("/pipayshopapi/bg-img")
@Slf4j
public class BgImgController {

    @Resource
    private BgImgService bgImgService;
    @Autowired
    private ImageMapper imageMapper;


    @PostMapping("logicDeleteBgImg/{bgId}")
    @ApiOperation("逻辑删除首页背景图片")
    public ResponseVO logicDeleteBgImg(@PathVariable String bgId){

        try{
            boolean result= bgImgService.logicDeleteBgImg(bgId);
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("逻辑删除首页背景图片成功");
        }catch (Exception e){
            log.error(e.getMessage());
            throw new BusinessException("background_image_deletion_failed");
        }
    }
    @GetMapping("selectBgImgList/{category}")
    @ApiOperation("查询网店 / 实体店首页轮播背景图列表")
    public ResponseVO<List<BgImgVO>> selectBgImgList(@PathVariable int category){
        try{
            List<BgImgVO> voList = bgImgService.selectBgImgList(category);
            return ResponseVO.getSuccessResponseVo(voList);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new BusinessException("background_image_query_failed");
        }
    }
    @PostMapping("addBgImg")
    @ApiOperation("新增首页背景轮播图")
    //Todo 后面要补全只对哪个服务器开放
    @CrossOrigin
    public ResponseVO addBgImg(BgImgDTO bgImgDTO){
        try{
            Boolean result= bgImgService.addBgImg(bgImgDTO);
            if (!result) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("新增首页背景轮播图成功");
        }catch (Exception e){
            log.error(e.getMessage());
            throw new BusinessException("carousel_addition_failed");
        }
    }

    /**
     * 网店首页的分类栏上传图片
     */
    @PostMapping("bgCategoryImage")
    @ApiOperation("网店、实体店首页的轮播图上传图片")
    //Todo 后面要补全只对哪个服务器开放
    @CrossOrigin
    public ResponseVO<String> bgCategoryImage(@RequestParam("file") MultipartFile multipartFile){
        try {
            String imageId = bgImgService.bgCategoryImage(multipartFile);
            return ResponseVO.getSuccessResponseVo(imageId);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("image_upload_failed");
        }
    }
    /**
     * 删除图片缓存
     */
    @DeleteMapping("deleteImage")
    @ApiOperation("删除图片缓存")
    @CrossOrigin
    public ResponseVO deleteImage(@RequestBody List<String> imageIds) {
        try {
            // 根据id获取origin_path存入数组中
            List<String> paths = imageMapper.selectPaths(imageIds);

            // 打印路径以进行调试
            log.info("删除路径集合: {}", paths);
            // 提取相对路径
            List<String> imagePaths = paths.stream()
                    .map(path -> path.replace("https://pi-pipayshop.oss-rg-china-mainland.aliyuncs.com/", ""))
                    .collect(Collectors.toList());
            OssUtil.deleteFiles(imagePaths);
            imageMapper.deleteBatchImageIds(imageIds);
            return ResponseVO.getSuccessResponseVo("删除图片缓存成功");
        } catch (Exception e) {
            log.error("Error deleting images: ", e);
            throw new BusinessException("image_cache_deletion_failed");
        }
    }

}
